package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.exception.CustomException;
import com.xjy.common.utils.PageUtils;
import com.xjy.system.domain.SysDeposit;
import com.xjy.system.domain.SysDepositUpdate;
import com.xjy.system.domain.bo.SysDepositUpdateAddBo;
import com.xjy.system.domain.bo.SysDepositUpdateEditBo;
import com.xjy.system.domain.bo.SysDepositUpdateQueryBo;
import com.xjy.system.domain.vo.SysDepositUpdateVo;
import com.xjy.system.mapper.SysDepositUpdateMapper;
import com.xjy.system.service.ISysDepositUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 押金变更信息Service业务层处理
 *
 * @author zc
 * @date 2022-02-17
 */
@Service
public class SysDepositUpdateServiceImpl extends ServicePlusImpl<SysDepositUpdateMapper, SysDepositUpdate> implements ISysDepositUpdateService {

//	@Autowired
//	private ISysDepositService sysDepositService;

	@Override
	public SysDepositUpdateVo queryById(Long id) {
		return getVoById(id, SysDepositUpdateVo.class);
	}

	@Override
	public TableDataInfo<SysDepositUpdateVo> queryPageList(SysDepositUpdateQueryBo bo) {
		PagePlus<SysDepositUpdate, SysDepositUpdateVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysDepositUpdateVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<SysDepositUpdateVo> queryList(SysDepositUpdateQueryBo bo) {
		return listVo(buildQueryWrapper(bo), SysDepositUpdateVo.class);
	}

	private LambdaQueryWrapper<SysDepositUpdate> buildQueryWrapper(SysDepositUpdateQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<SysDepositUpdate> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getDepId() != null, SysDepositUpdate::getDepId, bo.getDepId());
		lqw.eq(bo.getDepMaxType() != null, SysDepositUpdate::getDepMaxType, bo.getDepMaxType());
		lqw.eq(bo.getDepMinType() != null, SysDepositUpdate::getDepMinType, bo.getDepMinType());
		lqw.eq(bo.getDepAmt() != null, SysDepositUpdate::getDepAmt, bo.getDepAmt());
		lqw.eq(bo.getDepTime() != null, SysDepositUpdate::getDepTime, bo.getDepTime());
		lqw.eq(bo.getDepStatus() != null, SysDepositUpdate::getDepStatus, bo.getDepStatus());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(SysDepositUpdateAddBo bo) {
		SysDepositUpdate add = BeanUtil.toBean(bo, SysDepositUpdate.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(SysDepositUpdateEditBo bo) {
		SysDepositUpdate update = BeanUtil.toBean(bo, SysDepositUpdate.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SysDepositUpdate entity) {
		//TODO 做一些数据校验,如唯一约束

		SysDeposit sysDeposit = new SysDeposit();
//			sysDepositService.getBaseMapper().selectOne(new QueryWrapper<SysDeposit>().eq("id", entity.getDepId()));
		QueryWrapper<SysDepositUpdate> wrapper = new QueryWrapper<>();
		wrapper.eq("dep_id", entity.getDepId()); //name为 "Jone" 的用户
		List<SysDepositUpdate> sysDepositUpdates = getBaseMapper().selectList(wrapper);
		long sum = sysDeposit.getDepAmt().longValue();
		for (int i = 0; i < sysDepositUpdates.size(); i++) {
			sum -= sysDepositUpdates.get(i).getDepAmt();
		}
		if (sum < 0){
			throw new CustomException("操作失败，余额不足");
		}
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
